1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 using System.Security.Cryptography;
11 namespace WarehouseManagementSystem
12 {
13 public partial class frmStock : Form
14 {
15 SqlConnection con = null;
16 SqlCommand cmd = null;
17 SqlDataReader rdr;
18 ConnectionString cs = new ConnectionString();
19 public frmStock()
20 {
21 InitializeComponent();
22 }
23 public void FillCombo()
24 {
25 try
26 {
27
28 con = new SqlConnection(cs.DBConn);
29 con.Open();
30 string ct = "select RTRIM(SupplierName) from Supplier order by SupplierName";
31 cmd = new SqlCommand(ct);
32 cmd.Connection = con;
33 rdr = cmd.ExecuteReader();
34
35 while (rdr.Read())
36 {
37 cmbSupplierName.Items.Add(rdr[0]);
38 }
39 con.Close();
40 }
41 catch (Exception ex)
42 {
43 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
44 }
45 }
46 private void frmStock_Load(object sender, EventArgs e)
47 {
48
49 FillCombo();
50 }
51 public static string GetUniqueKey(int maxSize)
52 {
53 char[] chars = new char[62];
54 chars =
55 "123456789".ToCharArray();
56 byte[] data = new byte[1];
57 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
58 crypto.GetNonZeroBytes(data);
59 data = new byte[maxSize];
60 crypto.GetNonZeroBytes(data);
61 StringBuilder result = new StringBuilder(maxSize);
62 foreach (byte b in data)
63 {
64 result.Append(chars[b % (chars.Length)]);
65 }
66 return result.ToString();
67 }
68 private void auto()
69 {
70 txtStockID.Text = "ST-" + GetUniqueKey(6);
71 }
72
73 private void button2_Click(object sender, EventArgs e)
74 {
75 this.Hide();
76 frmProductsRecord1 frm = new frmProductsRecord1();
77 frm.lblUser.Text = lblUser.Text;
78 frm.lblUserType.Text = lblUserType.Text;
79 frm.Show();
80 }
81
82 private void btnSave_Click(object sender, EventArgs e)
83 {
84 if (txtProductName.Text == "")
85 {
86 MessageBox.Show("Please retrieve product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
87 txtProductName.Focus();
88 return;
89 }
90 if (txtQty.Text == "")
91 {
92 MessageBox.Show("Please enter quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
93 txtQty.Focus();
94 return;
95 }
96 if (cmbSupplierName.Text == "")
97 {
98 MessageBox.Show("Please select Supplier name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
99 cmbSupplierName.Focus();
100 return;
101 }
102 try
103 {
104 con = new SqlConnection(cs.DBConn);
105 con.Open();
106
107 string ct = "select ProductID from temp_Stock where ProductID='" + txtProductID.Text + "'";
108 cmd = new SqlCommand(ct);
109 cmd.Connection = con;
110 rdr = cmd.ExecuteReader();
111
112 if (rdr.Read())
113 {
114 con = new SqlConnection(cs.DBConn);
115 con.Open();
116 string cb2 = "Update Temp_Stock set Quantity=Quantity + " + txtQty.Text + " where ProductID='"+ txtProductID.Text + "'";
117 cmd = new SqlCommand(cb2);
118 cmd.Connection = con;
119 cmd.ExecuteReader();
120 con.Close();
121
122 }
123 else
124 {
125 con = new SqlConnection(cs.DBConn);
126 con.Open();
127 string cb1 = "insert into Temp_Stock(ProductID,Quantity) VALUES ('" + txtProductID.Text + "'," + txtQty.Text + ")";
128 cmd = new SqlCommand(cb1);
129 cmd.Connection = con;
130
131 cmd.ExecuteReader();
132 con.Close();
133 }
134 auto();
135 con = new SqlConnection(cs.DBConn);
136 con.Open();
137 string cb = "insert into Stock(StockID,ProductID,SupplierID,Quantity,StockDate,ExpiryDate) VALUES ('" + txtStockID.Text + "','" + txtProductID.Text + "','" + txtSupplierID.Text + "'," + txtQty.Text + ",@d1,@d2)";
138 cmd = new SqlCommand(cb);
139 cmd.Connection = con;
140 cmd.Parameters.AddWithValue("@d1", dtpStockDate.Text);
141 cmd.Parameters.AddWithValue("@d2", dtpExpiryDate.Text);
142 cmd.ExecuteReader();
143 con.Close();
144 MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
145 btnSave.Enabled = false;
146
147 }
148 catch (Exception ex)
149 {
150 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
151 }
152 }
153
154
155 private void Reset()
156 {
157
158 txtFeatures.Text = "";
159 txtProductName.Text = "";
160 txtQty.Text = "";
161 cmbSupplierName.Text = "";
162 txtStockID.Text = "";
163 dtpStockDate.Text = DateTime.Today.ToString();
164 dtpExpiryDate.Text = DateTime.Today.ToString();
165 btnDelete.Enabled = false;
166 btnUpdate.Enabled = false;
167 btnSave.Enabled = true;
168 }
169
170 private void btnNew_Click(object sender, EventArgs e)
171 {
172 Reset();
173 }
174
175 private void btnDelete_Click(object sender, EventArgs e)
176 {
177 if (MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
178 {
179 delete_records();
180 }
181 }
182 private void delete_records()
183 {
184
185 try
186 {
187
188 int RowsAffected = 0;
189 con = new SqlConnection(cs.DBConn);
190 con.Open();
191 string cb2 = "Update Temp_Stock set Quantity=Quantity - " + txtQty1.Text + " where ProductID='" + txtProductID.Text + "'";
192 cmd = new SqlCommand(cb2);
193 cmd.Connection = con;
194 cmd.ExecuteReader();
195 con.Close();
196 con = new SqlConnection(cs.DBConn);
197 con.Open();
198 string cq = "delete from Stock where StockID='" + txtStockID.Text + "'";
199 cmd = new SqlCommand(cq);
200 cmd.Connection = con;
201 RowsAffected = cmd.ExecuteNonQuery();
202 if (RowsAffected > 0)
203 {
204 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
205 Reset();
206 }
207 else
208 {
209 MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
210 Reset();
211 }
212 if (con.State == ConnectionState.Open)
213 {
214 con.Close();
215 }
216
217
218 }
219 catch (Exception ex)
220 {
221 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
222 }
223 }
224
225 private void frmStock_FormClosing(object sender, FormClosingEventArgs e)
226 {
227 this.Hide();
228 frmMainMenu frm = new frmMainMenu();
229 frm.lblUser.Text = lblUser.Text;
230 frm.lblUserType.Text = lblUserType.Text;
231 frm.Show();
232 }
233
234 private void btnGetData_Click(object sender, EventArgs e)
235 {
236 this.Hide();
237 frmStockRecord1 frm = new frmStockRecord1();
238 frm.lblUser.Text = lblUser.Text;
239 frm.lblUserType.Text = lblUserType.Text;
240 frm.Show();
241 frm.GetData();
242 }
243
244
245 private void btnUpdate_Click(object sender, EventArgs e)
246 {
247
248 if (txtProductName.Text == "")
249 {
250 MessageBox.Show("Please retrieve product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
251 txtProductName.Focus();
252 return;
253 }
254 if (txtQty.Text == "")
255 {
256 MessageBox.Show("Please enter quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
257 txtQty.Focus();
258 return;
259 }
260 if (cmbSupplierName.Text == "")
261 {
262 MessageBox.Show("Please select Supplier name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
263 cmbSupplierName.Focus();
264 return;
265 }
266 try
267 {
268 con = new SqlConnection(cs.DBConn);
269 con.Open();
270
271 string ct = "select ProductID from temp_Stock where ProductID='" + txtProductID.Text + "'";
272 cmd = new SqlCommand(ct);
273 cmd.Connection = con;
274 rdr = cmd.ExecuteReader();
275
276 if (rdr.Read())
277 {
278 con = new SqlConnection(cs.DBConn);
279 con.Open();
280 string cb2 = "Update Temp_Stock set Quantity=Quantity + " + txtQty.Text + " - "+ txtQty1.Text + " where ProductID='" + txtProductID.Text + "'";
281 cmd = new SqlCommand(cb2);
282 cmd.Connection = con;
283 cmd.ExecuteReader();
284 con.Close();
285
286 }
287 else
288 {
289 con = new SqlConnection(cs.DBConn);
290 con.Open();
291 string cb1 = "insert into Temp_Stock(ProductID,Quantity) VALUES ('" + txtProductID.Text + "'," + txtQty.Text + ")";
292 cmd = new SqlCommand(cb1);
293 cmd.Connection = con;
294
295 cmd.ExecuteReader();
296 con.Close();
297 }
298 con = new SqlConnection(cs.DBConn);
299 con.Open();
300 string cb = "Update Stock set ProductID='" + txtProductID.Text + "',SupplierID='" + txtSupplierID.Text + "',Quantity=" + txtQty.Text + ",StockDate= '" + dtpStockDate.Text + "',ExpiryDate='" + dtpExpiryDate.Text + "' where StockID='" + txtStockID.Text + "'";
301 cmd = new SqlCommand(cb);
302 cmd.Connection = con;
303 cmd.ExecuteReader();
304 con.Close();
305 MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
306 btnUpdate.Enabled = false;
307
308 }
309 catch (Exception ex)
310 {
311 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
312 }
313 }
314
315 private void txtQty_KeyPress(object sender, KeyPressEventArgs e)
316 {
317 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
318 {
319 e.Handled = false;
320 }
321 else
322 {
323 e.Handled = true;
324 }
325 }
326
327 private void cmbSupplierName_SelectedIndexChanged(object sender, EventArgs e)
328 {
329 try
330 {
331 con = new SqlConnection(cs.DBConn);
332
333 con.Open();
334 cmd = con.CreateCommand();
335
336 cmd.CommandText = "SELECT SupplierID from Supplier WHERE SupplierName = '" + cmbSupplierName.Text + "'";
337 rdr = cmd.ExecuteReader();
338
339 if (rdr.Read())
340 {
341 txtSupplierID.Text = rdr.GetString(0).Trim();
342 }
343 if ((rdr != null))
344 {
345 rdr.Close();
346 }
347 if (con.State == ConnectionState.Open)
348 {
349 con.Close();
350 }
351
352
353 }
354 catch (Exception ex)
355 {
356 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
357 }
358 }
359
360
361 }
362 }